# -*- coding: utf-8 -*-
"""
Created on Tue Mar 27 10:13:44 2018

@author:Devin

"""
from sklearn import metrics
import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.tools.plotting import radviz
from pandas.tools.plotting import parallel_coordinates
import matplotlib as mpl
from sklearn import preprocessing
from sklearn import linear_model
from sklearn.utils import shuffle
import numpy as np
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
from sklearn.linear_model import ElasticNetCV
from sklearn.linear_model import BayesianRidge
from sklearn.linear_model import ARDRegression
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.externals import joblib

mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

all_col = ['passflow_id', 'sts_time', 'num', 'shop_id', 'updatatime',
           'follow_rate', 'batch_num', 'sts_date']
use_col = ['num', 'shop_id','batch_num', 'sts_date']
use_cols = ['num','batch_num','weekday','follow_rate','weather']
max_min = ['num']
one_hot_columns = ['weekday','weather']
train_col = ['weekday_wk_0', 'weekday_wk_1',
       'weekday_wk_2', 'weekday_wk_3', 'weekday_wk_4', 'weekday_wk_5',
       'weekday_wk_6','weather_阵雪','weather_阵雨', 'weather_多云', 'weather_晴', 'weather_阴']#,'weather_阵雪''weather_阵雨', 'weather_多云', 'weather_晴', 'weather_阴','weather_阵雪','weather_阵雨'
# 双标图'follow_rate', 'num',
'''
检测每两两特征间的相关性
'''


def joint_viz(feat1, feat2, df):
    mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    ax = sns.jointplot(feat1, feat2, data=df, kind='reg', size=5)
    plt.xticks(rotation=60)
    plt.savefig('joint_viz', dpi=900)
    plt.show()


def load_data(path):
    file_box = os.listdir(path)
    load_tb = pd.read_csv(path+'\\'+file_box[0], sep=',',usecols = all_col)
    tb_weather = pd.read_csv(path+'\\'+file_box[1],sep=',')
    tb_weather.index = pd.to_datetime(tb_weather.time)
    load_tb.index = pd.to_datetime(load_tb.sts_date)
    load_tb['weather'] = [tb_weather.loc[x].weather for x in load_tb.index]
    load_tb['weekday'] = ['wk_'+str(x) for x in load_tb.index.weekday]
    min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
    shops = list(set(load_tb.shop_id));tb_list = [];all_df = pd.DataFrame()
    for shop in shops:
        global()[str(shop)] = load_tb.loc[(load_tb.shop_id == shop and load_tb.batch_num. >= 0)]
        global()[str(shop)]  = pd.get_dummies(pd.concat([global()[str(shop)].loc[:,['weekday','weather','follow_rate','batch_num']],pd.DataFrame(min_max_scaler.fit_transform(global()[str(shop)].loc[:,'max_min']),index = global()[str(shop)].index,columns='max_min')],axis=1))
        tb_list.append(global()[str(shop)].sort_index());all_df = all_df.append(global()[str(shop)].sort_index())
    return tb_list,all_df
#'weekday','weather',

    '''
    pp = tb_list[0].loc[:,use_cols]
    pp.corr()
    pp.index = pp.follow_rate
    plt.scatter(pp.index,pp.batch_num)
    '''


if __name__ == '__main__':
    path = 'C:\\Users\\gswte\Desktop\\data'
    tb_list,all_df = load_data(path);now = tb_list[2];now = now.sort_index()
    train = now.iloc[:-10];test = now.iloc[-10:]
    trainx = train.iloc[:-1].loc[:,train_col];testx = now.iloc[-10:-1].loc[:,train_col]
    trainy = train.iloc[1:].batch_num
#    trainy = trainx.iloc[:-2];testx = now.iloc[-10:-2]
    clf = linear_model.LinearRegression()
    x = train.loc[:,train_col];y = train.batch_num.iloc[1:-1]
    clf.fit(trainx,trainy)
    diabetes_X_test = testx
    diabetes_y_test_p = clf.predict(diabetes_X_test)
    diabetes_y_test = test.iloc[1:].batch_num
    print('Coefficients:\n',clf.coef_)
    print('the mean sqare error:%.2f' %np.mean(abs(clf.predict(diabetes_X_test)-diabetes_y_test)))
    print("MSE:",metrics.mean_squared_error(diabetes_y_test,clf.predict(diabetes_X_test)))
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,clf.predict(diabetes_X_test))))
    plt.scatter(clf.predict(diabetes_X_test),diabetes_y_test,color='black')
#    plt.plot(diabetes_X_test,clf.predict(diabetes_X_test),color='blue',linewidth=3)
    
    ridgecv = RidgeCV(alphas=[0.01, 0.1, 0.5, 1, 3, 5, 7, 10, 20, 100], cv=5)
    ridgecv.fit(trainx,trainy)
    print ("最优的alpha值: ", ridgecv.alpha_)
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,ridgecv.predict(diabetes_X_test))))
    tt = ridgecv.predict(diabetes_X_test)
    print('the mean sqare error:%.2f' %np.mean(abs(ridgecv.predict(diabetes_X_test)-diabetes_y_test)))
    
    lassocv = LassoCV(alphas=[0.01, 0.01, 0.5, 1, 3, 5, 7, 10, 20, 100], cv=5)
    lassocv.fit(trainx,trainy.values.ravel())
    print ("最优的alpha值: ", lassocv.alpha_)
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,lassocv.predict(diabetes_X_test))))
    print('the mean sqare error:%.2f' %np.mean(abs(lassocv.predict(diabetes_X_test)-diabetes_y_test)))
    
    elasticNetCV = ElasticNetCV(l1_ratio=0.7, alphas=[0.01, 0.1, 0.5, 1, 3, 5, 7, 10, 20, 100], cv=5)
    elasticNetCV.fit(trainx,trainy.values.ravel())
    print ("最优的alpha值: ", elasticNetCV.alpha_)
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,elasticNetCV.predict(diabetes_X_test))))
    print('the mean sqare error:%.2f' %np.mean(abs(elasticNetCV.predict(diabetes_X_test)-diabetes_y_test)))
    
    bayesianRidge = BayesianRidge()
    bayesianRidge.fit(trainx,trainy.values.ravel())
    print ("最优的alpha值: ", bayesianRidge.alpha_)
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,bayesianRidge.predict(diabetes_X_test))))
    print('the mean sqare error:%.2f' %np.mean(abs(bayesianRidge.predict(diabetes_X_test)-diabetes_y_test)))
    cc = bayesianRidge.predict(diabetes_X_test)
    
    ardRegression = ARDRegression()
    ardRegression.fit(trainx,trainy.values.ravel())
    print ("最优的alpha值: ", ardRegression.alpha_)
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,ardRegression.predict(diabetes_X_test))))
    print('the mean sqare error:%.2f' %np.mean(abs(ardRegression.predict(diabetes_X_test)-diabetes_y_test)))
    cc2 = ardRegression.predict(diabetes_X_test)
    joblib.dump(ardRegression, 'ardRegression.pkl')
    
    poly = PolynomialFeatures(interaction_only=True)
    model = make_pipeline(PolynomialFeatures(2), Ridge())
    model.fit(trainx,trainy.values.ravel())
    print("RMSE:",np.sqrt(metrics.mean_squared_error(diabetes_y_test,model.predict(diabetes_X_test))))
    print('the mean sqare error:%.2f' %np.mean(abs(model.predict(diabetes_X_test)-diabetes_y_test)))
    cc3 = model.predict(diabetes_X_test)
    
    
    
    
    